﻿
/// <reference path="_namespace.js" />
/// <reference path="Animation.js" />


/*****************************************************************************
Ajax library class definition. Kevin Robinson 2006.
Class: ajax.effects.ColourTransform
/*****************************************************************************/

ajax.lang.addNamespace("ajax.effects");

//-----------------------------------------------------------------------------
ajax.effects.ColourTransform = function(doc, c1, c2, stepMillis, maxIterations)
{    
    ajax.effects.ColourTransform.superConstructor.call(this, doc, stepMillis, Math.max(maxIterations, 1));
    
    this.color1 = c1;
    this.color2 = c2;
    this.color  = ajax.lang.safeNew("ajax.drawing.Colour", c1.r, c1.g, c1.b);           
    this.setup();
    this.callback = this.transform;    
    
    // Public events
    this.getEvent(this.window, "onTransform");
}

//-----------------------------------------------------------------------------
ajax.lang.extend("ajax.effects.ColourTransform", "ajax.effects.Animation"); // inherit
//-----------------------------------------------------------------------------
ajax.effects.ColourTransform.prototype.color1 = null;
ajax.effects.ColourTransform.prototype.color2 = null;
ajax.effects.ColourTransform.prototype.color  = null;

//-----------------------------------------------------------------------------
// overrides
ajax.effects.ColourTransform.prototype.start = function()
{
    this.setup();
    ajax.effects.ColourTransform.superClass.start.call(this);
}

//-----------------------------------------------------------------------------
ajax.effects.ColourTransform.prototype.setup = function()
{
    this.stepR = (this.color2.r - this.color1.r) / this.maxIterations;
    this.stepG = (this.color2.g - this.color1.g) / this.maxIterations;
    this.stepB = (this.color2.b - this.color1.b) / this.maxIterations;
        
}

//-----------------------------------------------------------------------------
ajax.effects.ColourTransform.prototype.transform = function()
{    
    this.onTransform.call(this, {color: this.color});    
    this.color.r += this.stepR;
    this.color.g += this.stepG;
    this.color.b += this.stepB;
    this.color.update();    
}

//-----------------------------------------------------------------------------
ajax.effects.ColourTransform.prototype.affectElement = function(ele)
{
    // Add a listener to affect this element
    var listener = function(e)
    {
        ele.style.backgroundColor = e.color.value;
        ele.style.color = e.color.getInverse().value;
    }
    
    this.getEvent(this.window, "onTransform").addListener(listener);        
    
    // Return a pointer to the listener (so it could be removed later if needed).
    return this.onElementTransformHandler;    
}

//-----------------------------------------------------------------------------
ajax.effects.ColourTransform.prototype.toString = function()
{
    return "ajax.effects.ColourTransform";
}

//-----------------------------------------------------------------------------
ajax.effects.ColourTransform.cast = function(obj)
{///<returns type="ajax.effects.ColourTransform"/>
    return obj;    
}
